/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    string smallestFromLeaf(TreeNode* root) {
        vector<string> path;
        string ans = "~";
        auto dfs = [&](this auto&& dfs, TreeNode* node, string cur)
        {
            if(node == nullptr) return;
            cur += char('a' + node->val);
            if(node->left == nullptr && node->right == nullptr)
            {
                reverse(cur.begin(), cur.end());
                if(cur < ans)
                    ans = cur;
            }

            dfs(node->left, cur);
            dfs(node->right, cur);
            cur.pop_back();
        };
    
        string cur;
        dfs(root, cur);

        return ans;
    }
};
